package huu.phong.sshchecker;

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.UserInfo;

public class Checker implements UserInfo {
	
	private String username;
	
	private String password;
	
	private String host;
	
	private boolean authenticated = false;
	
	public Checker() {
		
	}
	
	public Checker(String username, String password, String host) {
		this.username = username;
		this.password = password;
		this.host = host;
	}
	
	@Override
	public String getPassword() {
		authenticated = true;
		return password;
	}
	
	public boolean check(JSch jsch, int timeout){
		boolean ret = false;
		try {
			Session session = jsch.getSession(username, host, 22);
			session.setUserInfo(this);
			session.connect(timeout);
			boolean connected = session.isConnected();
			if (connected && authenticated) {
//				Channel channel = session.openChannel("shell");
//				PortOutputStream out = new PortOutputStream();
//				channel.setOutputStream(out);
//				channel.connect(timeout);
//				Thread.sleep(2000);
//				String resposne = out.getString().trim();
//				if (!resposne.isEmpty() && !resposne.contains(host) && !resposne.contains("lease reconnect")
//						&& !resposne.contains("his account is currently not available")
//						&& !resposne.contains("please enter correct username and password")
//						&& !resposne.contains("user session is in progress")) {
//					ret = true;
//				}
//				channel.disconnect();
				ret = true;
			}
			session.disconnect();
		} catch (Exception e) {
		}
		return ret;
	}
	
	public String toString() {
		return String.format("%s|%s|%s", host, username, password);
	}
	
	@Override
	public String getPassphrase() {
		return null;
	}

	@Override
	public boolean promptPassword(String message) {
		return true;
	}

	@Override
	public boolean promptPassphrase(String message) {
		return false;
	}

	@Override
	public boolean promptYesNo(String message) {
		return true;
	}

	@Override
	public void showMessage(String message) {
		
	}
}
